package com.cater.oa.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.oa.entity.OaMeetingSure;
import com.cater.oa.service.OaMeetingSureServiceImpl;
import com.cater.query.CommonQuery;

/**
 * 描述：会议确认控制层
 * 
 * @author yong
 * @date 2019/04/08
 */
@Controller
@RequestMapping("/meetingSure")
public class OaMeetingSureController {

	@Autowired
	private OaMeetingSureServiceImpl service;

	/**
	 * @会议确认管理列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list(String meetingId, Model model) {
		if (StringUtils.isNotEmpty(meetingId)) {
			model.addAttribute("meetingId", meetingId);
		}
		return "oa/meetingSure/list";
	}

	/**
	 * @会议确认管理数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询会议确认分页数据")
	public PageResult<OaMeetingSure> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<OaMeetingSure> result = service.findPageResult(query);
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("meetingSure:add")
	public String addView() {
		return "oa/meetingSure/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("meetingSure:add")
	@ResponseBody
	@SystemControllerLog(description = "添加会议确认")
	public JSONObject add(OaMeetingSure t, CommonQuery query) {
		JSONObject json = new JSONObject();
		String id = service.saves(t);
		if (StringUtils.isNotBlank(id)) {
			json.put("code", 200);
			json.put("message", "success");
		} else {
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("meetingSure:edit")
	public String editView(String id, Model model, CommonQuery query) {
		OaMeetingSure t = service.getById(id);
		model.addAttribute("meetingSure", t);
		return "oa/meetingSure/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("meetingSure:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改会议确认")
	public JSONObject update(OaMeetingSure t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			String id = t.getId();
			OaMeetingSure sure = null;
			if (StringUtils.isNotEmpty(id)) {
				sure = new OaMeetingSure();
				sure = service.getById(id);
				sure.setAttendee(t.getAttendee());
				String reason = t.getReason();
				if (StringUtils.isNotEmpty(reason)) {
					sure.setReason(reason);
				}
				service.update(sure);
				json.put("code", 200);
				json.put("message", "修改成功");
			}else {
				json.put("code", 400);
				json.put("message", "修改失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	@RequiresPermissions("meetingSure:delete")
	@SystemControllerLog(description = "删除会议确认")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	/**
	 * 查看视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "detailView")
	@RequiresPermissions("meetingSure:detail")
	public String detailView(String id, Model model, CommonQuery query) {
		OaMeetingSure t = service.getById(id);
		model.addAttribute("meetingSure", t);
		return "oa/meetingSure/detail";
	}

}